package db;

import java.io.Console;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSetMetaData;
import java.sql.DriverManager;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
import javax.sql.RowSet;
import models.*;
import voidlib.UserRole;
public class DbConnector
{
    private static String dbURL = "jdbc:derby://localhost:1527/sample;user=app;password=app";
    // jdbc Connection
    private static Connection conn = null;
    private static Statement stmt = null;
    static {
        createConnection();
    }
    
    private static void createConnection()
    {
        try
        {                  
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
            //Get a connection
            conn = DriverManager.getConnection(dbURL); 
        }
        catch (Exception except)
        {
            except.printStackTrace();
        }
    }
    
    private static void shutdown()
    {
        try
        {
            if (stmt != null)
            {
                stmt.close();
            }
            if (conn != null)
            {
                DriverManager.getConnection(dbURL + ";shutdown=true");
                conn.close();
            }           
        }
        catch (SQLException sqlExcept)
        {
            
        }

    }
    public static ResultSet ExecuteQuery(String query)
    {
        try{
        stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery(query);
        return result;
        }
        catch(SQLException ex){
            ex.printStackTrace();
            return null;
        }
    }
    
    public static void Execute(String query) 
    {
        try{
        stmt = conn.createStatement();
        stmt.execute(query);
        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
        }
    }
    public static void createTables ( )
    {
        
        //Execute("create table users (id INT, login varCHAR(20), pw varCHAR(20),title varCHAR(20), balance INT,role varchar(100))");
        
        //Execute("create table clan (id NUMBER(6), title VARCHAR2(20), owner_id(6))");
        //Execute("create table bank (user_id NUMBER(6), type VARCHAR2(20))");
        //Execute("create table bandit_bank (bank_id NUMBER(6), clan_id NUMBER(6), bman_id NUMBER(6)");
        //Execute("create table bandit_bandit (owner_id NUMBER(6), bandit_id NUMBER(6), created DATE  DEFAULT SYSDATE)");
        //Execute("create table porducts (id NUMBER(6), price NUMBER(6), owner_id(6))");
        //Execute("create table porducts_users (id NUMBER(6), user_id NUMBER(6),product_id NUMBER(6))");
        
        // CORRECT STUFF
        
        //Execute("create table bandit (user_id int, hp int, clan_id int)");
        //Execute("create table bandit_bandit (owner_id int, bandit_id int, created bigint, status varchar(100))");
        
        Execute("create table bandit_bandit ("
                + "id INT not null primary key  GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
                + "owner_id int, bandit_id int, created bigint, status varchar(100))");
        
        /*Execute("create table clan (id INT not null primary key " + 
                " GENERATED ALWAYS AS IDENTITY " + 
                "(START WITH 1, INCREMENT BY 1), title varCHAR(20), owner_id int)");*/
        
        /*Execute("create table users (id INT not null primary key " + 
                " GENERATED ALWAYS AS IDENTITY " + 
                "(START WITH 1, INCREMENT BY 1), login varCHAR(20), password varCHAR(20),title varCHAR(20), balance INT,role varchar(100))");*/
        
        /*Execute("create table credits (id INT not null primary key " + 
                " GENERATED ALWAYS AS IDENTITY " + 
                "(START WITH 1, INCREMENT BY 1), businessman_id int, bank_id int, amount int, created bigint, status varchar(20))");*/
       
        
    }
    
    public static Connection getConnection() {
        return conn;
    }
    
}    
 

